/*
 * Virtual Earth Screensaver
 * Keith Kinnan
 * 5/12/2007
 * 9/30/2006
 * 
 * Portions of this code is based on the Visual Studio 2005 Starter Kit RSS Screen Saver:
 * http://msdn.microsoft.com/vstudio/express/visualcsharp/starterkit/default.aspx#rssss
 */

using System;
using System.Configuration;
using System.Drawing;
using System.Windows.Forms;
using System.Resources;
using System.Globalization;

namespace VirtualEarthScreensaver
{
    partial class OptionsForm : Form
    {
        public OptionsForm()
        {
            InitializeComponent();

            try
            {
                // Load the current settings
                imageUpdateTimeControl.Value = Properties.Settings.Default.UpdateTime;
                rssFeedTextBox.Text = Properties.Settings.Default.RssFeedUri;
                FeedUpdateTime.Value = Properties.Settings.Default.FeedRefreshTime;
                noProxyRadioButton.Checked = Properties.Settings.Default.DoNotUseProxy;
                defaultBrowserProxyRadioButton.Checked = Properties.Settings.Default.UseDefaultProxy;
                useSpecificProxyRadioButton.Checked = Properties.Settings.Default.UseSpecificProxy; 
                proxyAddressTextBox.Text = Properties.Settings.Default.ProxyAddress;
                proxyPortTextBox.Text = Properties.Settings.Default.ProxyPort;
                bypassProxyCheckBox.Checked = Properties.Settings.Default.BypassProxyForLocal;
                proxyBypassListTextBox.Text = Properties.Settings.Default.ByPassAddresses;
                if (Screen.AllScreens.Length == 1)
                {
                    multipleMonitorGroupBox.Enabled = false;
                    onlyOneSceneRadioButton.Enabled = false;
                    stretchSceneRadioButton.Enabled = false;
                    showMultipleSceneRadioButton.Enabled = false;
                }
                else
                {
                    onlyOneSceneRadioButton.Checked = Properties.Settings.Default.OnlyOneScene;
                    stretchSceneRadioButton.Checked = Properties.Settings.Default.StretchScene;
                    showMultipleSceneRadioButton.Checked = Properties.Settings.Default.ShowMultipleScenes;
                }
            }
            catch (ConfigurationException)
            {
                ShowErrorMessage(Properties.Resources.StrErrorReadingSettings);
            }
        }

        // Update the apply button to be active only if changes 
        // have been made since apply was last pressed
        private void UpdateApply()
        {
            if (Properties.Settings.Default.UpdateTime != imageUpdateTimeControl.Value
                  || Properties.Settings.Default.RssFeedUri != rssFeedTextBox.Text
                  || Properties.Settings.Default.FeedRefreshTime != FeedUpdateTime.Value
                  || Properties.Settings.Default.DoNotUseProxy != noProxyRadioButton.Checked
                  || Properties.Settings.Default.UseDefaultProxy != defaultBrowserProxyRadioButton.Checked
                  || Properties.Settings.Default.UseSpecificProxy != useSpecificProxyRadioButton.Checked
                  || Properties.Settings.Default.ProxyAddress != proxyAddressTextBox.Text
                  || Properties.Settings.Default.ProxyPort != proxyPortTextBox.Text
                  || Properties.Settings.Default.BypassProxyForLocal != bypassProxyCheckBox.Checked
                  || Properties.Settings.Default.ByPassAddresses != proxyBypassListTextBox.Text
                  || Properties.Settings.Default.OnlyOneScene != onlyOneSceneRadioButton.Checked
                  || Properties.Settings.Default.StretchScene != stretchSceneRadioButton.Checked
                  || Properties.Settings.Default.ShowMultipleScenes != showMultipleSceneRadioButton.Checked)
            {
                applyButton.Enabled = true;
            }
            else
            {
                applyButton.Enabled = false;
            }
        }

        // Apply all the changes since apply button was last pressed
        private void ApplyChanges()
        {
            Properties.Settings.Default.RssFeedUri = rssFeedTextBox.Text;
            Properties.Settings.Default.UpdateTime = (uint)imageUpdateTimeControl.Value;
            Properties.Settings.Default.FeedRefreshTime = (uint)FeedUpdateTime.Value;
            Properties.Settings.Default.DoNotUseProxy = noProxyRadioButton.Checked;
            Properties.Settings.Default.UseDefaultProxy = defaultBrowserProxyRadioButton.Checked;
            Properties.Settings.Default.UseSpecificProxy = useSpecificProxyRadioButton.Checked;
            Properties.Settings.Default.ProxyAddress = proxyAddressTextBox.Text;
            Properties.Settings.Default.ProxyPort = proxyPortTextBox.Text;
            Properties.Settings.Default.BypassProxyForLocal = bypassProxyCheckBox.Checked;
            Properties.Settings.Default.ByPassAddresses = proxyBypassListTextBox.Text;
            Properties.Settings.Default.OnlyOneScene = onlyOneSceneRadioButton.Checked;
            Properties.Settings.Default.StretchScene = stretchSceneRadioButton.Checked;
            Properties.Settings.Default.ShowMultipleScenes = showMultipleSceneRadioButton.Checked;

            Properties.Settings.Default.Save();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                ApplyChanges();
            }
            catch (ConfigurationException)
            {
                ShowErrorMessage(Properties.Resources.StrErrorSavingSettings);
            }
            finally
            {
                Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            ApplyChanges();
            applyButton.Enabled = false;
        }

        // Check whether the user provided URI points to a valid RSS feed
        private void validateButton_Click(object sender, EventArgs e)
        {
            string currentRssFeedUrl = string.Empty;
            try
            {
                char[] delimiter = { ';' };
                string[] rssFeedUrls = rssFeedTextBox.Text.Split(delimiter);
                foreach (string rssFeedUrl in rssFeedUrls)
                {
                    if (rssFeedUrl.Trim().Length > 0)
                    {
                        currentRssFeedUrl = rssFeedUrl;
                        RssFeed.FromUri(rssFeedUrl);
                    }
                }

                MessageBox.Show(Properties.Resources.StrValidRssFeed, Properties.Resources.StrVEScreensaverTitle, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            }
            catch (System.Xml.XmlException)
            {
                ShowFeedErrorMessage(Properties.Resources.StrInvalidRssFeed, currentRssFeedUrl);
            }
            catch (System.UriFormatException)
            {
                ShowFeedErrorMessage(Properties.Resources.StrInvalidUrl, currentRssFeedUrl);
            }
            catch (System.Net.WebException)
            {
                ShowFeedErrorMessage(Properties.Resources.StrConnectionError, currentRssFeedUrl);
            }
        }

        private static void ShowFeedErrorMessage(string error, string feed)
        {
            ShowErrorMessage(string.Format(CultureInfo.InvariantCulture, error, feed));
        }

        private static void ShowErrorMessage(string str)
        {
            MessageBox.Show(str, Properties.Resources.StrVEScreensaverTitle, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
        }

        private void rssFeedTextBox_TextChanged(object sender, EventArgs e)
        {
            UpdateApply();
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            UpdateApply();
        }

        private void numericUpDown3_ValueChanged(object sender, EventArgs e)
        {
            UpdateApply();
        }

        private void noProxyRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            UpdateApply();
        }

        private void defaultBrowserProxyRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            UpdateApply();
        }

        private void useSpecificProxyRadioButton_CheckedChanged(object sender, EventArgs e)
        {

            if (useSpecificProxyRadioButton.Checked)
            {
                proxyAddressTextBox.Enabled = true;
                proxyPortTextBox.Enabled = true;
                proxyBypassListTextBox.Enabled = true;
                bypassProxyCheckBox.Enabled = true;
            }
            else
            {
                proxyAddressTextBox.Enabled = false;
                proxyPortTextBox.Enabled = false;
                proxyBypassListTextBox.Enabled = false;
                bypassProxyCheckBox.Enabled = false;
            }
            
            UpdateApply();
        }

        private void proxyAddressAndPortTextBox_TextChanged(object sender, EventArgs e)
        {
            UpdateApply();
        }

        private void bypassProxyCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            UpdateApply();
        }

        private void bypassProxyListTextBox_TextChanged(object sender, EventArgs e)
        {
            UpdateApply();
        }

        private void showMultipleSceneRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            UpdateApply();
        }

        private void stretchSceneRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            UpdateApply();
        }

        private void onlyOneSceneRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            UpdateApply();
        }
    }
}